using UnityEngine;
using System.Collections;

public class AssembleRobot : BuildingOperationJob {
	
	RobotComponent arms, legs, head, torso, tank;
	HaulingJob[] jobs;
	
	bool hauled;
	bool haulingStarted;
	
	public AssembleRobot(int nx, int ny, int nz, Arms narms, Legs nlegs, Head nhead, Torso ntorso, Tank ntank):base(nx, ny, nz){
		arms = narms;
		legs = nlegs;
		torso = ntorso;
		head = nhead;
		tank = ntank;
		
		hauled = false;
		haulingStarted = false;
		
		jobs = new HaulingJob[5];
		
		jobs[0] = new HaulingJob(arms, x, y, z, this);
		jobs[1] = new HaulingJob(legs, x, y, z, jobs[0]);
		jobs[2] = new HaulingJob(torso, x, y, z, jobs[1]);
		jobs[3] = new HaulingJob(head, x, y, z,jobs[2]);
		jobs[4] = new HaulingJob(tank, x, y, z, jobs[3]);
		
		ItemList.heads.Remove(head);
		ItemList.arms.Remove(arms);
		ItemList.legs.Remove(legs);
		ItemList.torsos.Remove(torso);
		ItemList.tanks.Remove(tank);
		
		jobLabel = "Assemble Robot";
		
	}
	
	public bool workOn(double workspeed, Robot robot){
		
		bool isDone = false;
		
		try {
			hauled = jobs[0].j.complete;
		} catch {
			hauled = false;
		}
		
		if (hauled){
			isDone = base.workOn(workspeed, robot);
		} else {
			if (!haulingStarted){
				haulingStarted = true;
				robot.assignJob(jobs[4], true);
			}
		}
		
		if (isDone){
			GameObject.DestroyObject(arms.item);
			GameObject.DestroyObject(legs.item);
			GameObject.DestroyObject(head.item);
			GameObject.DestroyObject(torso.item);
			GameObject.DestroyObject(tank.item);
			
			GameObject obj = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("RobotPrefab"));
			obj.transform.position = new Vector3(x, y, z);
			Robot r = (Robot)obj.GetComponent("Robot");
			r.starterate((Arms)arms, (Legs)legs, (Head)head, (Torso)torso, (Tank)tank);
		}
		return isDone;
		
	}
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
